package com.itheima.dubbo.api.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查询出来缘分值与当前用户最高的用户
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        query.with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

    @Override
    public PageResult queryRecommendUser(Long userId, Integer pageNum, Integer pagesize) {
        //1. 构建条件，查询总记录数
        Query query = new Query(Criteria.where("toUserId").is(userId));
        long count = mongoTemplate.count(query, RecommendUser.class);
        //2. 查询当前页需要的数据 List.
        query.skip((pageNum - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);

        return new PageResult(pageNum.longValue(), pagesize.longValue(), count, recommendUserList);
    }

    @Override
    public RecommendUser queryByUserId(Long userId, Long recommendUserId) {
        Query query = new Query(Criteria.where("toUserId").is(userId).and("userId").is(recommendUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null){
            recommendUser = new RecommendUser();
        recommendUser.setUserId(userId);
        recommendUser.setScore(95D);
    }
        return recommendUser;
}

    @Override
    public List<RecommendUser> queryCardsList(Long userId) {
        //1.查询你喜欢与不喜欢人的人,并且提取id出来。
        Query query = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikesList = mongoTemplate.find(query, UserLike.class);
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikesList, "likeUserId", Long.class);
        //2.根据当前用户查询推荐人得到所有的推荐人，但是需要排除上面的喜欢与不喜欢的id
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);
        TypedAggregation typedAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),Aggregation.sample(10));

        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(typedAggregation, RecommendUser.class);
        return aggregate.getMappedResults();
    }
}
